<?php

namespace app\admin\controller;
use app\admin\model\CommentDict as CommentDictModel;
use think\Db;

class CommentDict extends Base {

    protected $evaluate_config_model;

    public function _initialize() {
        parent::_initialize();
        $this->comment_dict_model = new CommentDictModel();
        $this->assign('sorceList',evaluateSorce());
    }

    /**
     * 评分配置列表
     * @param string
     * @param int
     * @return string
     */
    public function index() {
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $data = input('get.');
            $list = $this->comment_dict_model->getListInfo($page, $limit, $data);
            echo json_encode($list);
            exit;
        }
        return $this->fetch();
    }

    
    //删除评分配置
    public function del() {
        if (input('get.id')) {
            $id = input('get.id');
            //更新删除活动信息
            $data = Db::name('comment_dict')->where('id',$id)->delete();
            if ($data) {
                exit(json_encode(['status' => 1, 'msg' => '删除成功']));
            } else {
                exit(json_encode(['status' => 0, 'msg' => '删除失败']));
            }
        } else {
            $this->error('不存在的评分信息', '', '', 1);
        }
    }

    

    //添加评分配置
    public function add() {
        if (input('post.')) {
            $data = input('post.');
            $data['admin_id'] = session('admin_id');
            $data['add_time'] = time();
            $id = Db::name('comment_dict')->insertGetId($data);
            if ($id) {
                //Db::name('comment_dict')->where('id',$id)->delete();
                foreach ($data['label'] as $key=>$value){
                    if(!empty($value)){
                        Db::name('comment_label_dict')->insertGetId(['comment_id'=>$id,'rank'=>$data['rank'],'label'=>$value,'status'=>1,'add_time'=>time()]);
                    }
                }
                echo ajax_success('提交成功');
            } else {
                echo ajax_error('提交失败');
            }
        } else {
            return view();
        }
    }

    //修改评分配置
    public function edit() {
        if (input('post.')) {
            $data = input('post.');
            Db::name('comment_dict')->where('id', $data['id'])->update($data);
            foreach ($data['label'] as $key=>$value){
                if(!empty($value) && !empty($data['label_id'][$key])){
                    Db::name('comment_label_dict')->where(['id'=>$data['label_id'][$key]])->update(['rank'=>$data['rank'],'label'=>$value]);
                }else{
                    Db::name('comment_label_dict')->insertGetId(['comment_id'=>$data['id'],'rank'=>$data['rank'],'label'=>$value,'status'=>1,'add_time'=>time()]);
                }
            }
            echo ajax_success('编辑成功');
        } else {
            //查询评分信息
            $id = input('get.id');
            $info = Db::name('comment_dict')->where('id', $id)->find();
            //查询标签列表
            $list = Db::name('comment_label_dict')->where('comment_id', $id)->select();
            $this->assign('info', $info);
            $this->assign('list', $list);
            return view();
        }
    }
}
